package com.systop.controller;

import com.systop.pojo.Banner;
import com.systop.pojo.Goods_type;
import com.systop.service.GoodsTypeService;
import com.systop.unti.ResponseResult;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author zhangnan
 * @Data 2020/8/003 下午 4:54
 * @Descriiption
 **/
@RestController
@RequestMapping("/GoodsType")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;
    /**
     * 查询所有
     */
    @RequestMapping(value = "/selectGoodstypeAll", method = RequestMethod.POST)
    public ResponseResult selectGoodstypeAll(){
        return goodsTypeService.selectGoodsTypeAll();
    }
    /**
     * 添加商品分类
     */
    @RequestMapping(value = "/insertGoodstype", method = RequestMethod.POST)
    public ResponseResult insertGoodstype(@RequestParam("goods_type_name") String goods_type_name,
                                          @RequestParam(value = "file", required = false) MultipartFile[] file) throws IOException, JSONException {
        org.json.JSONObject res = new org.json.JSONObject();
        org.json.JSONObject resUrl = new JSONObject();
        List<String> imageurls = new ArrayList<>();
        for (MultipartFile files : file) {
            String filename = UUID.randomUUID().toString().replaceAll("-", "");
            String ext = FilenameUtils.getExtension(files.getOriginalFilename());
            String filenames = filename + "." + ext;
            System.out.println(filenames);
            String pathname = "F:\\idea-workspace\\hgtt\\admin\\src\\main\\resources\\GoodsType\\" + filenames;
            files.transferTo(new File(pathname));
            imageurls.add("GoodsType/" + filenames);
        }
        resUrl.put("src", imageurls);
        res.put("data", resUrl);
        String goods_type_photo = resUrl.get("src").toString();
        System.out.println(goods_type_photo);
        System.out.println(resUrl.get("src"));
        System.out.println(res.get("data"));
        Goods_type goods_type = new Goods_type();
        goods_type.setGoods_type_name(goods_type_name);
        goods_type.setGoods_type_photo(goods_type_photo);
        return goodsTypeService.insertGoodsType(goods_type);
    }
    /**
     * 修改商品分类
     */
    @RequestMapping(value = "/updateGoodstype", method = RequestMethod.POST)
    public ResponseResult updateGoodstype(@RequestParam("goods_type_name") String goods_type_name,
                                          @RequestParam(value = "file", required = false) MultipartFile[] file,
                                          @RequestParam("goods_type_id") Integer goods_type_id)throws IOException, JSONException{
        {
            org.json.JSONObject res = new org.json.JSONObject();
            org.json.JSONObject resUrl = new JSONObject();
            List<String> imageurls = new ArrayList<>();
            for (MultipartFile files : file) {
                String filename = UUID.randomUUID().toString().replaceAll("-", "");
                String ext = FilenameUtils.getExtension(files.getOriginalFilename());
                String filenames = filename + "." + ext;
                System.out.println(filenames);
                String pathname = "F:\\idea-workspace\\hgtt\\admin\\src\\main\\resources\\GoodsType\\" + filenames;
                files.transferTo(new File(pathname));
                imageurls.add("GoodsType/" + filenames);
            }
            resUrl.put("src", imageurls);
            res.put("data", resUrl);
            String GoodsType_photo = resUrl.get("src").toString();
            System.out.println(GoodsType_photo);
            System.out.println(resUrl.get("src"));
            System.out.println(res.get("data"));
           Goods_type goods_type = new Goods_type();
           goods_type.setGoods_type_id(goods_type_id);
           goods_type.setGoods_type_name(goods_type_name);
           goods_type.setGoods_type_photo(GoodsType_photo);
            return goodsTypeService.updateGoodsType(goods_type);

        }

    }
    /**
     * 修改商品分类之前根据查id查询
     */
    @RequestMapping(value = "/selectGoodstypeByid", method = RequestMethod.POST)
    public ResponseResult selectGoodstypeByid(@RequestParam("goods_type_id") Integer goods_type_id){
        return goodsTypeService.selectGoodsTypeByid(goods_type_id);

    }
    /**
     * 根据商品id删除
     */
    @RequestMapping(value = "/deleteGoodstypeByid", method = RequestMethod.POST)
    public ResponseResult deleteGoodstypeByid(@RequestParam("goods_type_id") Integer goods_type_id){
        return goodsTypeService.deleteBannerByid(goods_type_id);
    }


}
